using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class BillController : Controller
    {
        //
        // GET: /Bill/

        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        [SpiderE3Authorize(Roles = SiteRoles.Employee | SiteRoles.Admin)]
        public ActionResult Index()
        {
            foreach (var item in _db.HOA_DON)
            {
                item.CHI_TIET_HOA_DON.Load();
                item.NGUOI_DUNG1Reference.Load();
                item.NGUOI_DUNGReference.Load();
                item.PT_GIAO_HANGReference.Load();
                item.PT_THANH_TOANReference.Load();
                item.TRANG_THAIReference.Load();
            }

            return View(_db.HOA_DON.ToList());

            
        }
        [SpiderE3Authorize(Roles = SiteRoles.Employee | SiteRoles.Admin)]
        public ActionResult Details(int id)
        {
            var item = _db.HOA_DON.Where((c) => c.id == id).FirstOrDefault();
            item.CHI_TIET_HOA_DON.Load();
            item.NGUOI_DUNG1Reference.Load();
            item.PT_GIAO_HANGReference.Load();
            item.PT_THANH_TOANReference.Load();
            item.TRANG_THAIReference.Load();

            ViewData["SAN_PHAM"] = item.CHI_TIET_HOA_DON;
            ViewData["GOP_Y"] = _db.GOP_Y.Where((c) => c.SAN_PHAM.id == id);

            return View(item);
        }
        [SpiderE3Authorize(Roles = SiteRoles.Employee | SiteRoles.Admin)]
        public ActionResult Delete(HOA_DON billToDelete)
        {
            try
            {
                var bill = (from b in _db.HOA_DON
                                       where b.id == billToDelete.id
                                       select b).FirstOrDefault();
                _db.DeleteObject(bill);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }

        }
        //
        // GET: /Bill/Create
        [SpiderE3Authorize(Roles = SiteRoles.Member | SiteRoles.Employee | SiteRoles.Admin)]
        public ActionResult Create()
        {
            ViewData["PTGiaoHang"] = new SelectList(_db.PT_GIAO_HANG.ToList(), "id", "TenPhuongThuc");
            ViewData["PTThanhToan"] = new SelectList(_db.PT_THANH_TOAN.ToList(), "id", "TenPhuongThuc");          
            return View();
        }

        //
        // POST: /Bill/Create
        [SpiderE3Authorize(Roles=SiteRoles.Member | SiteRoles.Employee | SiteRoles.Admin)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude="id")]HOA_DON hdToCreate, FormCollection form)
        {
            try
            {
                hdToCreate.NgayDatHang = DateTime.Now;
                hdToCreate.NgayXacNhan = DateTime.Now;
                hdToCreate.TaiKhoanKhachHang = User.Identity.Name;
                //hdToCreate.TongTien = 0;
                //nguoi dat hang.
                NGUOI_DUNG kh = _db.NGUOI_DUNG.Where(nd => nd.TenDangNhap == User.Identity.Name).First();

                hdToCreate.NGUOI_DUNG1 = kh; //_db.NGUOI_DUNG.Where(nd => nd.TenDangNhap == User.Identity.Name).First();
                hdToCreate.NGUOI_DUNG = kh; // _db.NGUOI_DUNG.Where(nd => nd.TenDangNhap == User.Identity.Name).First();

                int ptgh = int.Parse(form.Get("idPTGH"));
                int pttt = int.Parse(form.Get("idPTTT"));
                
                hdToCreate.PT_GIAO_HANG = _db.PT_GIAO_HANG.Where(pt => pt.id == ptgh).First();
                hdToCreate.PT_THANH_TOAN = _db.PT_THANH_TOAN.Where(pt => pt.id == pttt).First();

                hdToCreate.TRANG_THAI = _db.TRANG_THAI.Where(tt => tt.id == _db.TRANG_THAI.Min(tt2 => tt2.id)).First();
           

                //them gio hang.
                //lay goi hang cuoi cung cua nguoi dung.
                kh.GIO_HANG.Load();
                int idmax = kh.GIO_HANG.Max(g2 => g2.id);
                GIO_HANG gio = kh.GIO_HANG.Where(g => g.id == idmax).First();

                gio.CHI_TIET_GIO_HANG.Load();
                foreach (var ct in gio.CHI_TIET_GIO_HANG)
                {
                    ct.SAN_PHAMReference.Load();
                    hdToCreate.CHI_TIET_HOA_DON.Add(
                        new CHI_TIET_HOA_DON {  SAN_PHAM = ct.SAN_PHAM,
                                                SoLuong = ct.SoLuong,
                                                DonGia = (short)ct.SAN_PHAM.Gia,
                                                GhiChu =  ""

                        });
                }

                hdToCreate.TongTien = hdToCreate.CHI_TIET_HOA_DON.Sum(ct => ct.DonGia * ct.SoLuong);
                hdToCreate.DonGia = (short)hdToCreate.TongTien;

                _db.AddToHOA_DON(hdToCreate);
                _db.SaveChanges();                  

                return RedirectToAction("ThanhToan");

            }
            catch (Exception ex)
            {
                ViewData["PTGiaoHang"] = new SelectList(_db.PT_GIAO_HANG.ToList(), "id", "TenPhuongThuc");
                ViewData["PTThanhToan"] = new SelectList(_db.PT_THANH_TOAN.ToList(), "id", "TenPhuongThuc");                    
                return View();
            }
        }

        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        public ActionResult Edit(int id)
        {
            var item = _db.HOA_DON.Where(hd=>hd.id==id).First();
            item.TRANG_THAIReference.Load();
            ViewData["TrangThai"] = new SelectList(_db.TRANG_THAI.ToList(), "id", "TenTrangThai", item.TRANG_THAI.id);
            return View(item);
        }

        //
        // POST: /Bill/Edit
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, short idTrangThai)
        {
            try
            {
                NGUOI_DUNG nd = _db.NGUOI_DUNG.Where(n => n.TenDangNhap == User.Identity.Name).First();
                HOA_DON originalHD = _db.HOA_DON.Where(hd=>hd.id==id).First();
                originalHD.TRANG_THAIReference.EntityKey = new System.Data.EntityKey("ComputerShopDBEntities1.TRANG_THAI", "id", idTrangThai);
                originalHD.NGUOI_DUNGReference.EntityKey = new System.Data.EntityKey("ComputerShopDBEntities1.NGUOI_DUNG", "id", nd.id);
                _db.SaveChanges();

                return RedirectToAction("Index");

            }
            catch (Exception ex)
            {
                var item = _db.HOA_DON.Where(hd => hd.id == id).First();
                item.TRANG_THAIReference.Load();
                ViewData["TrangThai"] = new SelectList(_db.TRANG_THAI.ToList(), "id", "TenTrangThai", item.TRANG_THAI.id);
                return View(item);
            }
        }
        public ActionResult ThanhToan()
        {
            return View();
        }
    }
}
